home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / devices and hardware / disks / iso9660 / i_o.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.3 KB  |  220 lines

  1. /*
  2.     File:        i_o.c
  3.     
  4.     Description:
  5.  
  6.     Author:        
  7.  
  8.     Copyright:     Copyright: © 1990-1999 by Apple Computer, Inc.
  9.                 all rights reserved.
  10.     
  11.     Disclaimer:    You may incorporate this sample code into your applications without
  12.                 restriction, though the sample code has been provided "AS IS" and the
  13.                 responsibility for its operation is 100% yours.  However, what you are
  14.                 not permitted to do is to redistribute the source as "DSC Sample Code"
  15.                 after having made changes. If you're going to re-distribute the source,
  16.                 we require that you make it clear in the source that the code was
  17.                 descended from Apple Sample Code, but that you've made changes.
  18.     
  19.     Change History (most recent first):
  20.                 6/24/99    Updated for Metrowerks Codewarror Pro 2.1(KG)
  21.  
  22. */
  23. #include <stdio.h>
  24. #include <Types.h>
  25. #include <ToolUtils.h>
  26. #include <Quickdraw.h>
  27. #include <Files.h>
  28. #include <Memory.h>
  29. #include <Devices.h>
  30. #include <MacMemory.h>
  31.  
  32. #define ISO
  33.  
  34. #include "HighSierra.h"
  35. #include "BuildISO.h"
  36.  
  37. #include "i_o.h"
  38. #include "Support.h"
  39. #include "ErrorMsg.h"
  40.  
  41.  
  42. /************************************************************************
  43.  *
  44.  *  Function:        isoOpen
  45.  *
  46.  *  Purpose:        Open the driver for i/o
  47.  *
  48.  *  Returns:        OSErr
  49.  *
  50.  *  Side Effects:    fills *RefNum with valid reference number for driver.
  51.  *
  52.  *  Description:    Issue Mac OS PBOpen call to open the driver.  We
  53.  *                    can use the returned reference number to access the
  54.  *                    driver for raw i/o.
  55.  *
  56.  *
  57.  ************************************************************************/
  58. OSErr
  59. isoOpen(StringPtr fn, short *RefNum)
  60. {
  61.     ParamBlockRec    pb;
  62.     OSErr    result;
  63.     
  64.     pb.ioParam.ioNamePtr = fn;
  65.     pb.ioParam.ioCompletion = 0;
  66.     pb.ioParam.ioPermssn = fsRdPerm;
  67.     result = PBOpen(&pb, false);
  68.     *RefNum = pb.ioParam.ioRefNum;
  69.     
  70.     return result;
  71. }
  72.  
  73. /************************************************************************
  74.  *
  75.  *  Function:    isoRead
  76.  *
  77.  *  Purpose:    read from the driver
  78.  *
  79.  *  Returns:    OSErr
  80.  *
  81.  *  Side Effects:    fills dest with contents for 'count' bytes.
  82.  *
  83.  *  Description:    do a call to the Mac CD driver to read a specified
  84.  *                    number of bytes starting at 'offset'.  The number of
  85.  *                    bytes must be a multiple of 512, or this call won't
  86.  *                    work (the driver returns an error)
  87.  *
  88.  ************************************************************************/
  89. OSErr
  90. isoRead(short referenceNumber, Ptr dest, long count, long offset)
  91. {
  92.     OSErr result;
  93.     ParamBlockRec    io;
  94.     
  95.     ClearOut((char *)&io, sizeof(io));
  96.     io.ioParam.ioCompletion = NULL;
  97.     io.ioParam.ioVRefNum = 0;
  98.     io.ioParam.ioRefNum = referenceNumber;
  99.     io.ioParam.ioBuffer = dest;
  100.     io.ioParam.ioReqCount = count;
  101.     io.ioParam.ioPosMode = fsFromStart;
  102.     io.ioParam.ioPosOffset = offset;
  103.     result = PBRead(&io, false);
  104.     
  105.     if (result != noErr)
  106.         ErrorMsg("PBRead failed. ioActCount = %d", io.ioParam.ioActCount);
  107.     
  108.     return result;
  109. }
  110.  
  111.  
  112. /************************************************************************
  113.  *
  114.  *  Function:    isoWrite
  115.  *
  116.  *  Purpose:    Write to the driver
  117.  *
  118.  *  Returns:    OSErr
  119.  *
  120.  *  Side Effects:    none
  121.  *
  122.  *  Description:    do a call to the Mac CD driver to write a specified
  123.  *                    number of bytes starting at 'offset'.  The number of
  124.  *                    bytes must be a multiple of 512, or this call won't
  125.  *                    work (the driver returns an error.)
  126.  *
  127.  ************************************************************************/
  128. OSErr
  129. isoWrite(short referenceNumber, Ptr buffer, long count, long offset)
  130. {
  131.     OSErr result;
  132.     ParamBlockRec    io;
  133.     
  134.     ClearOut((char *)&io, sizeof(io));
  135.     io.ioParam.ioCompletion = NULL;
  136.     io.ioParam.ioVRefNum = 1;
  137.     io.ioParam.ioRefNum = referenceNumber;
  138.     io.ioParam.ioBuffer = buffer;
  139.     io.ioParam.ioReqCount = count;
  140.     io.ioParam.ioPosMode = fsFromStart;
  141.     io.ioParam.ioPosOffset = offset;
  142.     result = PBWrite(&io, false);
  143.     
  144.     if (result != noErr)
  145.         ErrorMsg("PBWrite failed. result = %d", result);
  146.     
  147.     return result;
  148. }
  149.  
  150.  
  151. /************************************************************************
  152.  *
  153.  *  Function:        ZeroDisk
  154.  *
  155.  *  Purpose:        Clean out this disk so it's not HFS anymore
  156.  *
  157.  *  Returns:        OSErr
  158.  *
  159.  *  Side Effects:    zero out a lot of disk
  160.  *
  161.  *  Description:    loop, doing a write of zeros.
  162.  *
  163.  ************************************************************************/
  164. OSErr
  165. ZeroDisk(short referenceNumber)
  166. {
  167.     Ptr        noBuffer;
  168.     short    i;
  169.     short    j;
  170.     OSErr    result;
  171.     CursHandle    cursor;
  172.     
  173.     noBuffer = NewPtr(CDBLKSIZE);
  174.     if (noBuffer == NULL)
  175.         return (MemError());
  176.     for (i = 0; i < CDBLKSIZE; i++)
  177.         noBuffer[i] = 0;
  178.  
  179.     result = noErr;
  180.  
  181.      cursor = GetCursor(watchCursor);
  182.      if (!cursor)
  183.          SetCursor(*cursor);
  184.     for (i = 0, j = 0; i < 10 && result == noErr; i++, j++)
  185.         result = isoWrite(referenceNumber, noBuffer, (long)CDBLKSIZE, (long) j*CDBLKSIZE);
  186.     
  187.     SetCursor(&qd.arrow);
  188.     DisposePtr(noBuffer);
  189.     return result;
  190. }
  191.  
  192. /************************************************************************
  193.  *
  194.  *  Function:        GetDriveNumber
  195.  *
  196.  *  Purpose:        Get the driver number
  197.  *
  198.  *  Returns:        short.  The driver number
  199.  *
  200.  *  Side Effects:    none.
  201.  *
  202.  *  Description:    PBHGetVInfo() will retrieve the driver
  203.  *                    number, given the vRefNum associated with a file.
  204.  *
  205.  ************************************************************************/
  206. short
  207. GetDriveNumber(short vRefNum)
  208. {
  209.     HParamBlockRec    io;
  210.     
  211.     io.volumeParam.ioCompletion = NULL;
  212.     io.volumeParam.ioNamePtr = NULL;
  213.     io.volumeParam.ioVRefNum = vRefNum;
  214.     io.volumeParam.ioVolIndex = 0;
  215.     PBHGetVInfo(&io, false);
  216.     return io.volumeParam.ioVDrvInfo;
  217. }
  218.  
  219.  
  220.